//
//  UITableView+Extension.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/17.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import Foundation
extension UITableView {

    class func adjustedTableView(frame: CGRect = CGRect.zero,
                                 style: UITableViewStyle = .plain,
                                 viewController: UIViewController,
                                 insets: UIEdgeInsets = UIEdgeInsets.zero,
                                 hiddenFooter: Bool = true) -> UITableView {

        let tableView = UITableView(frame: frame, style: style)
        tableView.contentInset = insets

        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
            tableView.estimatedRowHeight = 0 //防止闪烁
            tableView.estimatedSectionFooterHeight = 0
            tableView.estimatedSectionHeaderHeight = 0
        } else {
            viewController.automaticallyAdjustsScrollViewInsets = false
        }
        if hiddenFooter {
            tableView.tableFooterView = UIView()
        }
        return tableView
    }

    func hideFooter() {
        tableFooterView = UIView()
    }

    func hideTopMargin() {
        tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
    }

    func registerCellClass(with type: UITableViewCell.Type, extraIDs: [String] = []) {
        if extraIDs.isEmpty {
            register(type, forCellReuseIdentifier: String(describing: type))
        } else {
            for id in extraIDs {
                register(type, forCellReuseIdentifier: id)
            }
        }
    }

    func registerCellNib(with type: UITableViewCell.Type) {
        let name = String(describing: type)
        register(UINib(nibName: name, bundle: nil), forCellReuseIdentifier: name)
    }

    func registerHeaderFooterClass(with type: UITableViewHeaderFooterView.Type, extraIDs: [String] = []) {
        if extraIDs.isEmpty {
            register(type, forHeaderFooterViewReuseIdentifier: String(describing: type))
        } else {
            for id in extraIDs {
                register(type, forHeaderFooterViewReuseIdentifier: id)
            }
        }
    }

    func dequeueReusableCell<T: UITableViewCell>(for indexPath: IndexPath) -> T {
        let identifier = String(describing: T.classForCoder())
        guard let cell = self.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as? T else {
            fatalError("Could not dequeue cell with identifier: \(identifier)")
        }
        return cell
    }

    func dequeueReusableHeaderFooter<T: UITableViewHeaderFooterView>() -> T {
        let identifier = String(describing: T.classForCoder())
        guard let headerFooter = self.dequeueReusableHeaderFooterView(withIdentifier: identifier) as? T else {
            fatalError("Could not dequeue headerFooter with identifier: \(identifier)")
        }
        return headerFooter
    }
}


extension UICollectionView {

    func registerCellClass(with type: UICollectionViewCell.Type, extraIDs: [String] = []) {
        if extraIDs.isEmpty {
            register(type, forCellWithReuseIdentifier: String(describing: type))
        } else {
            for id in extraIDs {
                register(type, forCellWithReuseIdentifier: id)
            }
        }
    }

    func registerCellNib(with type: UICollectionViewCell.Type) {
        let name = String(describing: type)
        register(UINib(nibName: name, bundle: nil), forCellWithReuseIdentifier: name)
    }

    func dequeueReusableCell<T: UICollectionViewCell>(for indexPath: IndexPath) -> T {
        let identifier = String(describing: T.classForCoder())
        guard let cell = self.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as? T else {
            fatalError("Could not dequeue cell with identifier: \(identifier)")
        }
        return cell
    }
}




